package cn.bigcontainer.alt.calendar.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.bigcontainer.alt.calendar.dto.MCalendarEvent;
import cn.bigcontainer.alt.calendar.service.MCalendarService;
import cn.bigcontainer.alt.common.exception.BigResultCode;
import cn.bigcontainer.alt.common.util.BigConstant;
import cn.bigcontainer.alt.meta.dto.MAppDto;

/**
 * 前端采用fullCalendar3.10，实现appType=calendar
 * @author jiaod
 *
 */
@Controller
@RequestMapping("/meta/" + MAppDto.TYPE_CALENDAR)
@SessionAttributes(BigConstant.SESSION_USER)
public class MCalendarController {

	@Autowired
	private MCalendarService calendarService;

	@RequestMapping
	public String loadCalendar(MAppDto app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		MAppDto appDto = calendarService.loadCalendar(app.getId(), req);
		appDto.setDomprefix(app.getDomprefix());
		map.put("app", appDto);
		return "meta/" + MAppDto.TYPE_CALENDAR;
	}

	/**
	 * 加载视图时间范围内的事件
	 * @param subAppIds
	 * @param start
	 * @param end
	 * @param app
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public BigResultCode loadList(String[] subAppIds, String start, String end, MAppDto app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		List<MCalendarEvent> events = calendarService.loadList(app.getId(), subAppIds, start, end, req);
		return BigResultCode.getSuccessCode().setContent(events);
	}
	
	/**
	 * 响应eventDrop事件，event.start、event.end改变
	 * @param e
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/start")
	@ResponseBody
	public BigResultCode start(MCalendarEvent e, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		MCalendarEvent event = calendarService.start(e, req);
		return BigResultCode.getSuccessCode().setContent(event);
	}
	
	/**
	 * 响应eventResize事件，event.end改变
	 * @param e
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/end")
	@ResponseBody
	public BigResultCode end(MCalendarEvent e, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		MCalendarEvent event = calendarService.end(e, req);
		return BigResultCode.getSuccessCode().setContent(event);
	}
}
